import { defineStore } from 'pinia'
import { refresh, queryUserInfo, userActivity } from '@/api/index'
import { setStorageSync, getStorageSync, getLocation, reLaunch } from '@tarojs/taro'

export const useUserStore = defineStore('user', {
    persist: true, // 开启当前模块持久化
    state: () => ({
        token: getStorageSync('token')||'',
        location: {
            latitude: '',
            longitude: '',
        },
        info: {
            id: null,
            avatarUrl: '',
            is_star: false,
            is_photo_man: false,
            nickName: '',
            star_total: 0, 
            star_withdraw: 0,
            star_level: null,
            photo_total: 0, 
            photo_withdraw: 0,
           
        },
        tiktok:{
            d_id: null,
            d_avatar: '',
            dy_nickname: '',
            dy_link: '',
            mini_link:'',
            is_authorization_expires: false,
        },
        mode: getStorageSync('mode')||1, // 模式 1达人 2摄影师
        beforeLoginUrl: '', // 登陆前的页面地址 /pages/home/index
        beforeAuthUrl: '',  // 认证前的页面地址 /pages/home/index
    }),
    getters: {
        isLogin: state => !!state.token,
        total: state => Number(state.mode==1 ? state.info.star_total : state.info.photo_total), // 累计奖励
        withdraw: state => Number(state.mode==1 ? state.info.star_withdraw : state.info.photo_withdraw), // 已提现奖励
        balance(){
            return parseFloat((this.total - this.withdraw).toFixed(2)) // 可提现奖励
        },
    },
    actions: {
        setUserToken(str){
            this.token = str
            setStorageSync('token', str)
        },
        logOut(){
            this.setUserToken('')
        },
        switchMode(num){
            this.mode = num
            setStorageSync('mode', num)
            reLaunch({url: '/pages/home/index'});
        },
        setBeforeLoginUrl(url){
            this.beforeLoginUrl = url
        },
        setBeforeAuthUrl(url){
            this.beforeAuthUrl = url
        },
        setUserInfo(obj){
            const keyArr = Object.keys(obj)
            for(const item of keyArr){
                this.info[item] = obj[item]
            }
        },
        getLocalInfo(){
            // this.mode = getStorageSync('mode')
            // this.token = getStorageSync('token')
            // 获取本地信息
            const info = getStorageSync('info')
            this.setUserInfo(info)
        },
        async updataLocation(){
            // 获取用户当前地理位置
            try {
                const res = await getLocation({ type: "wgs84" })
                const { latitude, longitude } = res;
                if(latitude){
                    this.location = { latitude, longitude};
                }
                return res;
            } catch (error) {
                throw error
            }
        },
        async refreshToken(){
            try {
                const res = await refresh()
                // 更新token
                this.setUserToken(res.data.token)
                return res.data.token
            } catch (error) {
                throw error
            }
        },
        async getUserInfo(){
            if(!this.isLogin) return false
            try {
                const res = await queryUserInfo(this.location)
                if(!res.data) return false
                const { mini_avatar, mini_nickname, id, area, is_photo_man, is_star, is_wait,
                    photo_is_submit, photo_total, photo_withdraw, 
                    star_is_submit, star_total, star_withdraw, star_level, 
                    d_id, d_avatar, dy_nickname, dy_link, fans_num, favour_num, is_authorization_expires,mini_link } = res.data;
                this.setUserInfo({
                    id,
                    area,
                    avatarUrl: decodeURIComponent(mini_avatar),
                    nickName: mini_nickname,
                    photo_is_submit, photo_total, photo_withdraw,
                    star_is_submit, star_total, star_withdraw, star_level,
                    is_star, is_photo_man, is_wait
                })
                this.tiktok = { d_id, d_avatar, dy_nickname, dy_link, fans_num, favour_num, is_authorization_expires,mini_link }
                return id
            } catch (error) {
                throw error
            }
        },
        async userActivityStatus(id){
            try {
                if(!this.isLogin) return false
                const res = await userActivity(id)
                return res.data ? res.data: false
            } catch (error) {
                throw error
            }
        },
    }
})

export default useUserStore